package com.example.win.android_banduyujia.peopleactivity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.win.android_banduyujia.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Win on 2015/9/8.
 */
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    public String[] group ={"基础","理疗", "流派"};
    public String[][] gridViewChild = { {"塑形", "保养"},{"悲痛修复","焦虑头痛"},{"艾扬格", "哈他", "新流派3"}};
    public String[][]  child = { {" "},{" "}, {" "} };

    private LayoutInflater  mInflater;
    private Context    context;

    public ExpandableListViewAdapter(Context  context) {
         mInflater = LayoutInflater.from(context);
         this.context = context;
    }

    @Override
    public int getGroupCount() {
        return group.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView == null){
            mViewChild = new ViewChild();
            convertView=mInflater.inflate(R.layout.view_item_expandable_list, null);
            mViewChild.textView = (TextView)convertView.findViewById(R.id.channel_group_name);
            mViewChild.imageView=(ImageView)convertView.findViewById(R.id.channel_imageview_orientation);
            convertView.setTag(mViewChild);
        }else{
            mViewChild=(ViewChild)convertView.getTag();
        }

        if(isExpanded){
            mViewChild.imageView.setImageResource(R.mipmap.channel_expandablelistview_top_icon);
        }else{
            mViewChild.imageView.setImageResource(R.mipmap.channel_expandablelistview_bottom_icon);
        }
        mViewChild.textView.setText(getGroup(groupPosition).toString());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
            mViewChild = new ViewChild();
            convertView = mInflater.inflate(R.layout.view_item_expandable_channel, null);
            mViewChild.gridView = (GridView)convertView.findViewById(R.id.channel_item_child_gridView);
            convertView.setTag(mViewChild);
        }else {
            mViewChild=(ViewChild)convertView.getTag();
        }
        SimpleAdapter  mSimpleAdapter = new SimpleAdapter(context, setGirdViewData(gridViewChild[groupPosition]),
                R.layout.view_channel_gridview_item, new String[]{"channel_gridview_item"}, new int[]{R.id.channel_gridview_item});
        mViewChild.gridView.setAdapter(mSimpleAdapter);
        setGridViewListener(mViewChild.gridView);
        mViewChild.gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        return convertView;
    }

    /**
     * 设置gridview点击事件监听
     * @param gridView
     */
    public void setGridViewListener(GridView gridView) {
        gridView.setOnItemClickListener(new GridView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 if(view instanceof TextView){
                     // 如果想要获取到哪一行，则自定义gridview的adapter，item设置2个textview一个隐藏设置id，显示哪一行
                     TextView tv = (TextView) view;
                     Toast.makeText(context, "position=" + position + "||" + tv.getText(), Toast.LENGTH_SHORT).show();
                     Log.e("hefeng", "gridView listaner position=" + position + "|| text=" +tv.getText());
                 }
            }
        });
    }

    /**
     * 设置gridview数据
     * @param data
     * @return
     */
    public ArrayList<HashMap<String, Object>> setGirdViewData(String[] data) {
        ArrayList<HashMap<String, Object>>  gritItem = new ArrayList<HashMap<String, Object>>();
        for (int i =0; i<data.length; i++){
            HashMap<String, Object>  hashMap = new HashMap<String, Object>();
            hashMap.put("channel_gridview_item", data[i]);
            gritItem.add(hashMap);
        }
        return gritItem;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    ViewChild  mViewChild;

    static class ViewChild{
        ImageView  imageView;
        TextView   textView;
        GridView   gridView;
    }
}
